package TAIC.feature;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;


public class MarkFeature {
	int len = 3 ;
	public static int colors [] = new int [] {
		Color.RED.getRGB(), 
		Color.BLUE.getRGB(),
		Color.GREEN.getRGB(),
		Color.CYAN.getRGB(),
		Color.BLACK.getRGB(),
		Color.MAGENTA.getRGB(),
		Color.orange.getRGB(),
		Color.YELLOW.getRGB() } ;
	
	static public void main ( String args [ ])	throws Exception {
		if ( args.length == 0 ) { 
			System.out.println ( "please input the point file name" ) ;
			System.out.println ( "or the config file and number of top features to show" ) ;
			return ;
		}
		if (args.length == 2  ) ( new MarkFeature() ).markFolders ( args ) ;
		else if ( args.length == 1 )( new MarkFeature() ).markOneFile ( ".\\", args[0], 30 ) ;
	}
	
	public void markFolders ( String args[]) throws Exception {
		Scanner scanner = new Scanner ( new File ( args[ 0 ])) ;
		int top = Integer.valueOf( args[ 1 ]) ;
		while ( scanner.hasNext() ) markOneFolder ( scanner.nextLine().trim(), top ) ; 
	}
	
	public void markOneFolder ( String folder, int top ) throws Exception {
		Scanner scanner = new Scanner ( new File ( folder + "\\index.txt" )) ;
		while ( scanner.hasNext() )  markOneFile ( folder, scanner.nextLine().trim() + ".point", top );
		scanner.close() ;
	}
	
	public void markOneFile ( String folder, String pointFile, int top) throws Exception  { 
		Scanner scanner = new Scanner ( new File ( folder + "\\" + pointFile )) ;
		String fileName = scanner.nextLine().trim() ;
		BufferedImage bi = getImg ( folder + "\\" + fileName ) ;
		for ( int i = 0 ; i < top && scanner.hasNextDouble() ; i ++ ) {
			double y = scanner.nextDouble() ; 
			double x = scanner.nextDouble() ;
			int color = scanner.nextInt() ; 
			Point point = new Point ( x, y, color ) ;
			markPoint ( bi, point ) ;
			if ( scanner.hasNextLine() ) scanner.nextLine () ;
		}
		scanner.close() ;
		writeImage ( bi, folder + "\\" + "!" + fileName ) ; 
	}	
	
	private void markPoint ( BufferedImage bi, Point point ) {
		int cx = (int)point.x ;
		int cy = (int)point.y ;
		
		int w = bi.getWidth() ;
		int h = bi.getHeight() ; 

		int color = Color.WHITE.getRGB() ;
		if ( point.color < colors.length ) color = colors [ point.color ] ;  
		int l1 = cx - len >= 0 ? cx - len : 0 ;
		int r1 = cx + len < w  ? cx + len : w - 1 ;
		int l2 = cy - len >= 0 ? cy - len : 0 ;
		int r2 = cy + len < h  ? cy + len : h - 1 ;
		
		for ( int i = l1 ; i <= r1 ; i ++ )
			for ( int j = l2 ; j <= r2; j ++ ) bi.setRGB( i, j, color ) ;
	}
	
	private BufferedImage getImg ( String fileName ) throws Exception {
		try {
			BufferedImage img = ImageIO.read(new File( fileName ));
			return img ; 	
			
		} catch ( Exception e ) {
			System.out.println ( "Turning to grayScale image error: " + fileName ) ;
			return null; 
		}
	}

	private void writeImage ( BufferedImage bi, String fileName) throws Exception {
		ImageIO.write( bi , "jpg", new FileImageOutputStream ( new File ( fileName )));		
	}

	
}

class Point {
	double x , y ;
	int color;
	
	public Point ( double x, double y, int color ) {
		this.x = x;
		this.y = y ; 
		this.color = color ; 
	}
	
}
